package zheezes.eisp.autotask.view;

import java.util.Calendar;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.springframework.context.support.AbstractApplicationContext;

import zheezes.eisp.ImageRegistryFactory;
import zheezes.eisp.LocaleResources;
import zheezes.eisp.autotask.model.AutotaskManager;
import zheezes.eisp.util.SpringUtil;
import zheezes.util.DateFormatUtil;
import zheezes.util.Log4jUtil;
import zheezes.util.Message;


public class AutotaskWindow implements Observer {
	private static Logger logger = Logger.getLogger(AutotaskWindow.class);
	
	private AutotaskManager autoaskManager;
	
	private ResourceBundle resourceBundle;
	private ImageRegistry imageRegistry;
	private Display display;
	private Shell shell;
	private Tray tray;
	private TrayItem item;
	private ToolTip tip;
	
	public static void main(String[] args) {
		PropertyConfigurator.configure("log4j.properties");
		Log4jUtil.setLogFile("autotask.log");
		AbstractApplicationContext context = SpringUtil.getApplicationContext();
		try {
			AutotaskWindow window = (AutotaskWindow) context.getBean("autotaskWindow");
			window.open();
		} catch (Exception e) {
			logger.info(e.getMessage(), e);
		}
		context.destroy();
	}

	public void open() {
		final Display display = Display.getDefault();
		createContents();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	protected void createContents() {
		shell = new Shell();

		if ((display = Display.getCurrent()) == null) {
			display = Display.getDefault();
		}
		tray = display.getSystemTray();
		if (tray != null) {
			item = new TrayItem(tray, SWT.NONE);
			tip = new ToolTip(shell, SWT.BALLOON);
			tip.setAutoHide(true);
			
			item.setToolTipText(resourceBundle
					.getString(LocaleResources.PROGRAM_AUTOTASK_TITLE));
			item.setImage(imageRegistry
					.get(ImageRegistryFactory.IMAGE_AUTOTASK));
			item.setToolTip(tip);

			item.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event e) {
					tip.setText(resourceBundle.getString(LocaleResources.VAR_AUTOTASK_SCHEDULE));
					tip.setMessage(DateFormatUtil.getDateFmt().format(autoaskManager.getSchedule().getTime()));
					tip.setVisible(true);
				}
			});
			final Menu menu = new Menu(shell, SWT.POP_UP);
			MenuItem exitMenu = new MenuItem(menu, SWT.PUSH);
			exitMenu.setText(resourceBundle.getString(LocaleResources.ACTION_EXIT));
			exitMenu.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					autoaskManager.stop();
					tip.dispose();
					item.dispose();
					tray.dispose();
					shell.dispose();
				}
			});
			item.addListener(SWT.MenuDetect, new Listener() {
				public void handleEvent(Event e) {
					menu.setVisible(true);
				}
			});
		}
		new Thread(autoaskManager).start();
	}

	public void setResourceBundle(ResourceBundle resourceBundle) {
		this.resourceBundle = resourceBundle;
	}

	public void setImageRegistry(ImageRegistry imageRegistry) {
		this.imageRegistry = imageRegistry;
	}
	
	public void setAutotaskManager(AutotaskManager autotaskManager) {
		this.autoaskManager = autotaskManager;
		autotaskManager.addObserver(this);
	}

	public void update(Observable o, Object arg) {
		if(arg != null) {
			Message msg = (Message) arg;
			Calendar cal = (Calendar) msg.getData();
			final String dateStr = DateFormatUtil.getDateFmt().format(cal.getTime());
			switch (msg.getType()) {
			case AutotaskManager.CREATE_SUCCESS:
				display.asyncExec(new Runnable(){
					public void run() {
						tip.setVisible(false);
						tip.setText(resourceBundle.getString(LocaleResources.VAR_CREATE_SUCCESS));
						tip.setMessage(dateStr + resourceBundle.getString(LocaleResources.VAR_TASK));
						tip.setVisible(true);
					}					
				});
				break;
			}
		} else {
			display.asyncExec(new Runnable(){
				public void run() {
					tip.dispose();
					item.dispose();
					tray.dispose();
					shell.dispose();
				}
			});
		}
	}
}
